package pers.chy.chyappointmentplatform.Controller;


import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.chy.chyappointmentplatform.Model.Ms_BrdaDTO;
import pers.chy.chyappointmentplatform.Model.NoMapping.ReplyMsgDTO;
import pers.chy.chyappointmentplatform.Model.Yjyy_ZsxxExtDTO;
import pers.chy.chyappointmentplatform.Service.LydjService;
import pers.chy.chyappointmentplatform.Utils.ChyException;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController     // 使用此注解才可以返回html
@CrossOrigin        // 允许跨域请求
@RequestMapping("LydjController")
public class LydjController {

    @Autowired
    private LydjService lydjService;

    /**
     * 登记来访者信息
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("recordVisitorInfo")
    public String recordVisitorInfo(@RequestBody Map params, HttpServletRequest request) {
        ReplyMsgDTO replyMsgDTO = new ReplyMsgDTO();

        // 日志
        Logger log = LoggerFactory.getLogger(LydjController.class);
        log.info("[Controller入参]：" + params);

        /* 进入Service层处理业务，根据是否发生异常判断业务是否成功 */
        try {
            String ygdm=(String)request.getAttribute("ygdm"); // 从JWT获取当前操作人员
            String ygxm=(String)request.getAttribute("ygxm");
            String ygksmc=(String)request.getAttribute("ksmc");
            String ygzc=(String)request.getAttribute("ygzc");


            String ret = lydjService.doRecordVisitorInfo(params,ygdm);

            // 业务成功
            replyMsgDTO.setStatus(true);
            replyMsgDTO.setDetail(ret);

        } catch (Exception ex) {
            // 业务失败
            replyMsgDTO.setStatus(false);
            replyMsgDTO.setDetail(ex.getMessage());
            // 异常日志
            if (ex instanceof ChyException) log.error("[错误]" + ex.getMessage());
            else log.error("[不期望的异常]", ex);
        }
        String replyMsgStr = new Gson().toJson(replyMsgDTO);
        log.info("[Controller出参]：" + replyMsgStr);
        return replyMsgStr;
    }

    @RequestMapping("getBrxx")
    public String getBrxx(@RequestBody Map params, HttpServletRequest request) {
        ReplyMsgDTO replyMsgDTO = new ReplyMsgDTO();

        // 日志
        Logger log = LoggerFactory.getLogger(LydjController.class);
        log.info("[Controller入参]：" + params);

        /* 进入Service层处理业务，根据是否发生异常判断业务是否成功 */
        try {
            String ygdm=(String)request.getAttribute("ygdm"); // 从JWT获取当前操作人员
            String ygxm=(String)request.getAttribute("ygxm");
            String ygksmc=(String)request.getAttribute("ksmc");
            String ygzc=(String)request.getAttribute("ygzc");


            Ms_BrdaDTO ret  = lydjService.doGetBrxx(params);

            // 业务成功
            replyMsgDTO.setStatus(true);
            replyMsgDTO.setDetail(new Gson().toJson(ret));

        } catch (Exception ex) {
            // 业务失败
            replyMsgDTO.setStatus(false);
            replyMsgDTO.setDetail(ex.getMessage());
            // 异常日志
            if (ex instanceof ChyException) log.error("[错误]" + ex.getMessage());
            else log.error("[不期望的异常]", ex);
        }
        String replyMsgStr = new Gson().toJson(replyMsgDTO);
        log.info("[Controller出参]：" + replyMsgStr);
        return replyMsgStr;
    }
}
